# -*- coding: utf-8 -*-

# @Project : DDT_FrameWork
# @Author  : Mr.Deng
# @Time    : 2021/8/26 21:26

"""
创建测试套件，执行测试用例，生成测试报告
"""

from common.tools import HTMLTestRunnerCN
from common.tools.logConfig import Logger
from common.tools.sendEmail import SendEmail
from common.tools.createTestSuits import create_test_suits
from common.tools.checkDriverVersion import CheckDriverVersion
from common.tools.checkRequirements import CheckRequirements
from common.tools.operateConfigData import OperateConfigData
from common.tools.timeTools import TimeTools as TT

from config.filePathConfig import *

cf = OperateConfigData(projectConfigPath).read_config_data()


def create_test_report(testSuits):
    """
    生成自动化测试报告
    :param testSuits: 测试用例套件
    :return:
    """
    title = cf.get("report", "title")
    description = cf.get("report", "description")
    reportName = reportSavePath + title + TT.get_now_date("%y%m%d%H%M%S") + ".html"

    # 写入测试报告文件
    with open(reportName, 'wb') as f:
        HTMLTestRunnerCN.HTMLTestRunner(
            stream=f, title=title, description=description
        ).run(testSuits)
    Logger().origin_logger.info(f"生成测试报告：{reportName}")


def start_run():
    """
    获取测试用例套件，执行测试用例，发送结果邮件
    :return:
    """
    try:
        # 检测测试环境，谷歌驱动，依赖安装
        # CheckDriverVersion().check_driver_version()
        # CheckRequirements().check_requirements()
        # 创建测试套件执行
        testSuits = create_test_suits()
        create_test_report(testSuits)
    except Exception:
        raise Exception("测试用例执行异常程序中断！！！")
    else:
        SendEmail().send_email()


if __name__ == '__main__':
    start_run()
